package commands;

import util.*;
import main.InputHandler;

public class ReadCommand extends Command {
	public static String name = "read";
	public static String help = "Name: read \n>read {str:opt(NULL,str x)} \n>read (client name(optional) \n>Reads a message from a client port or the current server";
	
	public static boolean test(InputHandler handler, StringList words){
		if(StringChecker.testWord(words, 0, "read")){
			if(words.size() > 1){
				if(OpenPortCommand.sockets.containsKey(words.get(1)) && OpenPortCommand.sockets.get(words.get(1)).connected()){
					Object o = OpenPortCommand.sockets.get(words.get(1)).read();
					if(o != null)
						handler.terminal.write((String)o);
					else
						handler.terminal.write("Nothing to read");
					return true;
				}
				handler.terminal.write("No socket to read from");
				return true;
			}else{
				if(ConnectCommand.socket != null && ConnectCommand.socket.connected()){
					Object o = ConnectCommand.socket.read();
					if(o != null)
						handler.terminal.write((String)o);
					else
						handler.terminal.write("Nothing to read");
					return true;
				}
			}
		}
		return false;
	}
	
	public static void process(Object o){
		
	}

}
